package com.example.board.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.TemporalAdjusters;
import java.util.*;

public class DateQuery {
    /**
     * 获取本周的第一天
     *
     * @return String
     **/
    public static String getWeekStart() {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.WEEK_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_WEEK, 2);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    public static String getNowStart() {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(new Date());
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.SECOND, 0);
        Date zero = calendar.getTime();;
        return new SimpleDateFormat("yyyy-MM-dd").format(zero) + " 00:00:00";//
    }
    /**
     * 获取本月的第一天
     *
     * @return String
     **/
    public static String getMothStart() {

        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 0);
        cal.set(Calendar.DAY_OF_MONTH, 1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }


    /**
     * 获取本月的最后一天
     *
     * @return String
     **/
    public static String getMothEnd() {

        Calendar cal = Calendar.getInstance();
//        cal.add(Calendar.DAY_OF_MONTH, 0);
//        cal.set(Calendar.DAY_OF_MONTH, 1);
        cal.roll(Calendar.DAY_OF_MONTH, -1);
        Date time = cal.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }


    /**
     * 获取上月的最后一天
     *
     * @return String
     **/
    public static String getMothEndLast() {

        //获取上个月的最后一天
        Calendar cal_2 = Calendar.getInstance();
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        Date time = cal_2.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取上月的第一天一天
     *
     * @return String
     **/
    public static String getMothStartLast() {

        //获取上个月的第一天
        Calendar cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -1);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        Date time = cal_1.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取上月的第一天一天
     *
     * @return String
     **/
    public static String getMothStart3() {

        //获取上个月的第一天
        Calendar cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -3);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        Date time = cal_1.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取上上月的最后
     *
     * @return String
     **/
    public static String getMothEndLast3() {

        //获取上上个月的最后
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.MONTH, -3);
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        Date time = cal_2.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取上上月的第一天一天
     *
     * @return String
     **/
    public static String getMothStartLast2() {

        //获取上上个月的第一天
        Calendar cal_1=Calendar.getInstance();//获取当前日期

        cal_1.add(Calendar.MONTH, -2);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        Date time = cal_1.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }



    /**
     * 获取上上月的最后
     *
     * @return String
     **/
    public static String getMothEndLast2() {

        //获取上上个月的最后
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.MONTH, -1);
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        Date time = cal_2.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取第四月的第一天
     *
     * @return String
     **/
    public static String getMothStartLast4() {

        Calendar cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -4);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        Date time1 = cal_1.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time1) + " 00:00:00";//
    }

    /**
     * 获取上上月的最后
     *
     * @return String
     **/
    public static String getMothEndLast4() {

        //获取上上个月的最后
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.MONTH, -4);
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        Date time = cal_2.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }


    /**
     * 获取上上月的最后
     *
     * @return String
     **/
    public static String getMothEndLast5() {

        //获取上上个月的最后
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.MONTH, -4);
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        Date time = cal_2.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取第四月的第一天
     *
     * @return String
     **/
    public static String getMothStartLast5() {

        Calendar cal_1=Calendar.getInstance();//获取当前日期
        cal_1.add(Calendar.MONTH, -5);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        Date time1 = cal_1.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time1) + " 00:00:00";//
    }

    /**
     * @param startMonth @example: "2016-01"
     * @param endMonth   @example: "2019-11"
     * @return 两个时间之间的月份(含开始, 结束)
     * @throws ParseException
     */
    public static List<Date> getMonths(String startMonth, String endMonth) throws ParseException {
        LinkedList<Date> months = new LinkedList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar minCalender = Calendar.getInstance();
        Calendar maxCalender = Calendar.getInstance();

        // 设置开始月份
        minCalender.setTime(sdf.parse(startMonth));
        minCalender.set(minCalender.get(Calendar.YEAR), minCalender.get(Calendar.MONTH), 1);

        maxCalender.setTime(sdf.parse(endMonth));
        // 设置日期,保证最后一个日期参数 大于 开始时间日历
        maxCalender.set(maxCalender.get(Calendar.YEAR), maxCalender.get(Calendar.MONTH), 2);

        while (minCalender.before(maxCalender)) {
            months.add(minCalender.getTime());
            minCalender.add(Calendar.MONTH, 1);
        }
        return months;
    }


    /**
     * 获取本年的第一天
     *
     * @return String
     **/

    public static String getYearStart() {

        return new SimpleDateFormat("yyyy").format(new Date()) + "-01-01";

    }


    /**
     * 获取某段时间内的周一(二等等)的日期
     *
     * @param dataBegin 开始日期
     * @param dataEnd   结束日期
     * @param weekDays  获取周几，1－6代表周一到周六。0代表周日
     * @return 返回日期List
     */

    public static List getDayOfWeekWithinDateInterval(String dataBegin, String dataEnd, int weekDays) {
//    public static List getDayOfWeekWithinDateInterval(String dataBegin, String dataEnd) {

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");

        List dateResult = new ArrayList<>();

        Calendar cal = Calendar.getInstance();

        String[] dateInterval = {dataBegin, dataEnd};

        Date[] dates = new Date[dateInterval.length];

        for (int i = 0; i < dateInterval.length; i++) {

            String[] ymd = dateInterval[i].split("[^\\d]+");

            cal.set(Integer.parseInt(ymd[0]), Integer.parseInt(ymd[1]) - 1, Integer.parseInt(ymd[2]));

            dates[i] = cal.getTime();

        }

        for (Date date = dates[0]; date.compareTo(dates[1]) <= 0; ) {

            cal.setTime(date);

//            if (cal.get(Calendar.DAY_OF_WEEK) - 1 == weekDays) {
            if (cal.get(Calendar.DAY_OF_WEEK) - 1 <= weekDays) {

                String format = sdf.format(date);

                dateResult.add(format);

            }

            cal.add(Calendar.DATE, 1);

            date = cal.getTime();

        }
        return dateResult;
    }



    /**
     * @param startMonth @example: "2016-01"
     * @param endMonth   @example: "2019-11"
     * @return 两个时间之间的月份(含开始, 结束)
     * @throws ParseException
     */
    public static  List<Date> getMonthDates(String startMonth, String endMonth) throws ParseException {
        LinkedList<Date> months = new LinkedList<>();

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
        Calendar minCalender = Calendar.getInstance();
        Calendar maxCalender = Calendar.getInstance();

//        Sun Oct 02 02:02:00 GMT+08:00 2022 周日 10月 2020
        System.out.println("开始时间： "+startMonth);
        // 设置开始月份
        minCalender.setTime(sdf.parse(startMonth));
        //设置周一
        minCalender.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
        System.out.println("开始时间： "+minCalender.getTime());

//        maxCalender.setTime(sdf.parse(endMonth));

        // 设置日期,保证最后一个日期参数 大于 开始时间日历
        maxCalender.set(Calendar.DAY_OF_WEEK, 27);
        System.out.println("结束时间： "+minCalender.getTime());
        System.out.println("结束时间： "+endMonth);

        while (minCalender.before(maxCalender)) {
            months.add(minCalender.getTime());
            minCalender.add(Calendar.DAY_OF_WEEK, 1);
        }//2022-10-  周日 00：00：00
        System.out.println(months.toString());
        return months;
    }


    /**
     * 根据指定日期获得一周的日期区间（周一和周日日期字符串表示）

     * @return
     */
    public static String getThisWeekTimeInterval(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return getNWeekTimeInterval(1, dateStr,format);
    }

    /**
     * 根据指定日期获得最近n周的日期区间（包含本周）
     * @param n
     * @param sdf
     * @return
     */
    public static String getNWeekTimeInterval(int n,String dateStr, SimpleDateFormat sdf) {
        Date date = null;
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String beginDate = getFromToDate(sdf, date, n, 0, 0);
        String endDate = getFromToDate(sdf, date, n, 1, 0);
        return beginDate + "," + endDate;
    }




    /**
     * 获取起止日期
     * @param sdf 需要显示的日期格式
     * @param date 需要参照的日期
     * @param n 最近n周
     * @param option 0 开始日期；1 结束日期
     * @param k 0 包含本周 1 不包含本周
     * @return
     */
    public static String getFromToDate(SimpleDateFormat sdf, Date date, int n, int option, int k) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1  + k) * 7 : offset - k * 7;
        calendar.add(Calendar.DATE, amount);
        return sdf.format(calendar.getTime());
    }

    /**
     * 获取起止日期
     * @param sdf 需要显示的日期格式
     * @param date 需要参照的日期
     * @param n 最近n周
     * @param option 0 开始日期；1 结束日期
     * @param k 0 包含本周 1 不包含本周
     * @return
     */
    public static String getFromToDate1(SimpleDateFormat sdf, Date date, int n, int option, int k) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        int offset = 0 == option ? 1 - dayOfWeek : 7 - dayOfWeek;
        int amount = 0 == option ? offset - (n - 1  + k) * 7 : offset - k * 7;
        calendar.add(Calendar.DATE, amount);
        return sdf.format(calendar.getTime());
    }

//    //调用
//    String[] dateArray = DateHelper.getThisWeekTimeInterval(logDate).split(",");

    public static Date[] getWeekDay() throws ParseException {

        Calendar calendar = Calendar.getInstance();

//        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
        while (calendar.get(Calendar.DAY_OF_WEEK) != Calendar.MONDAY) {
            calendar.add(Calendar.DAY_OF_WEEK, -1);
        }
        Date[] dates = new Date[7];  // new Date[7] 星期日
        for (int i = 0; i < 7; i++) {  // i < 7 星期日
            dates[i] = calendar.getTime();
            calendar.add(Calendar.DATE, 1);
        }
        return dates;
    }


    //通过上课时间 数字 周 1 返回 文字周一
    public  static List<String> week(List<String> num) {
        List<String> week = new ArrayList<>();
        for (String s : num) {
            if (s.contains("1")) {
                s = "周一";
                week.add(s);
            }
            if (s.contains("2")) {
                s = "周二";
                week.add(s);
            }
            if (s.contains("3")) {
                s = "周三";
                week.add(s);
            }
            if (s.contains("4")) {
                s = "周四";
                week.add(s);
            }
            if (s.contains("5")) {
                s = "周五";
                week.add(s);
            }
            if (s.contains("6")) {
                s = "周六";
                week.add(s);
            }
            if (s.contains("0")) {
                s = "周日";
                week.add(s);
            }
        }
        return week;
    }
    /**
     * 计算两个日期之间相隔的天数
     *
     * @param begin
     * @param end
     * @return
     * @throws ParseException
     */
    public static long countDay(String begin, String end) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date beginDate, endDate;
        long day = 0;
        try {
            beginDate = format.parse(begin);
            endDate = format.parse(end);
            day = (endDate.getTime() - beginDate.getTime()) / (24 * 60 * 60 * 1000);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return day;

    }

    //获取上月第一天
    public static String getLastMoth(){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Calendar lastStartCalendar = Calendar.getInstance();
        lastStartCalendar.add(Calendar.MONTH, -1);
        lastStartCalendar.set(Calendar.DAY_OF_MONTH,1);
        String lastStartDate = format.format(lastStartCalendar.getTime());
        return lastStartDate;
    }


    /**
     * 获取上上月的第一天一天
     *
     * @return String
     **/
    public static String getMothStartLast3() {

        //获取上上个月的第一天
        Calendar cal_1=Calendar.getInstance();//获取当前日期

        cal_1.add(Calendar.MONTH, -3);
        cal_1.set(Calendar.DAY_OF_MONTH,1);//设置为1号
        cal_1.set(Calendar.HOUR_OF_DAY,0);
        cal_1.set(Calendar.MINUTE,0);
        cal_1.set(Calendar.SECOND,0);
        Date time = cal_1.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取上上月的最后
     *
     * @return String
     **/
    public static String getMothEndLastt3() {

        //获取上上个月的最后
        Calendar cal_2 = Calendar.getInstance();
        cal_2.add(Calendar.MONTH, -2);
        cal_2.set(Calendar.DAY_OF_MONTH,0);//设置为1号,当前日期既为本月第一天
        cal_2.set(Calendar.HOUR_OF_DAY,23);
        cal_2.set(Calendar.MINUTE,59);
        cal_2.set(Calendar.SECOND,59);
        Date time = cal_2.getTime();
        return new SimpleDateFormat("yyyy-MM-dd").format(time) + " 00:00:00";//
    }

    /**
     * 获取某年第一天日期开始时刻
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirstDay(int year){
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        Date yearFirstDay = cal.getTime();
        return getStartDate(yearFirstDay);
    }

    /**
     * 获取某年最后一天日期最后时刻
     * @param year 年份
     * @return Date
     */
    public static Date getYearLastDay(int year){
        Calendar cal = Calendar.getInstance();
        cal.clear();
        cal.set(Calendar.YEAR, year);
        cal.roll(Calendar.DAY_OF_YEAR, -1);
        Date yearLastDay = cal.getTime();
        return getFinallyDate(yearLastDay);
    }


    public static SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
    public static SimpleDateFormat format1 = new SimpleDateFormat(
            "yyyyMMdd HH:mm:ss");

    /**
     * 得到指定日期的一天的开始时刻00:00:00
     *
     * @param date
     * @return
     */
    public static Date getStartDate(Date date) {
        String temp = format.format(date);
        temp += " 00:00:00";

        try {
            return format1.parse(temp);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 得到指定日期的一天的的最后时刻23:59:59
     *
     * @param date
     * @return
     */
    public static Date getFinallyDate(Date date) {
        String temp = format.format(date);
        temp += " 23:59:59";

        try {
            return format1.parse(temp);
        } catch (Exception e) {
            return null;
        }
    }

    /**
     * 获取当年的第一天
     * @param
     * @return
     */
    public static Date getCurrYearFirst(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearFirst(currentYear);
    }

    /**
     * 获取当年的最后一天
     * @param
     * @return
     */
    public static Date getCurrYearLast(){
        Calendar currCal=Calendar.getInstance();
        int currentYear = currCal.get(Calendar.YEAR);
        return getYearLast(currentYear);
    }

    /**
     * 获取某年最后一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearLast(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        calendar.roll(Calendar.DAY_OF_YEAR, -1);
        Date currYearLast = calendar.getTime();

        return currYearLast;
    }

    /**
     * 获取某年第一天日期
     * @param year 年份
     * @return Date
     */
    public static Date getYearFirst(int year){
        Calendar calendar = Calendar.getInstance();
        calendar.clear();
        calendar.set(Calendar.YEAR, year);
        Date currYearFirst = calendar.getTime();
        return currYearFirst;
    }


    /**
     * 获取某月第一天日期
     * @param  "202207"; // 指定年月
     * @return Date
     */
    public static LocalDate getMothSomeStart(String dateStr){

        LocalDate date = LocalDate.parse(dateStr + "01", DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate dateFirst = date.with(TemporalAdjusters.firstDayOfMonth()); // 指定年月的第一天

        return dateFirst;
    }

    /**
     * 获取某月第一天日期
     * @param  "202207"; // 指定年月
     * @return Date
     */
    public static LocalDate getMothSomeEnd(String dateStr){
        LocalDate date = LocalDate.parse(dateStr + "01", DateTimeFormatter.BASIC_ISO_DATE);
        LocalDate dateEnd = date.with(TemporalAdjusters.lastDayOfMonth()); // 指定年月的最后一天

        return dateEnd;
    }

    /**
     * 获取当天的最开始时间
     */

    /**
     * 获取当天0点 ----00:00:00
     * @return
     */
    public static LocalDateTime getTodayStartTime(String time){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime startDay = LocalDateTime.parse(time, df);
        return LocalDateTime.of(startDay.toLocalDate(), LocalTime.MIN);
    }

    public static LocalDateTime getTodayStartTime(LocalDateTime time){
        return LocalDateTime.of(time.toLocalDate(), LocalTime.MIN);
    }

    /**
     * 获取当天24点----23:59:59
     * @return
     */
    public static LocalDateTime getTodayEndTime(String time){
        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        LocalDateTime endDay = LocalDateTime.parse(time, df);

        return LocalDateTime.of(endDay.toLocalDate(), LocalTime.MAX);
    }
    public static LocalDateTime getTodayEndTime(LocalDateTime time){
        return LocalDateTime.of(time.toLocalDate(), LocalTime.MAX);
    }


}
